<?php
namespace Api\Controller;
use Think\Controller;

class MineController extends BaseController {

   private $m = NULL;
   private $md = NULL;
   private $navName = '订单管理';


   public function _initialize(){
   		parent::_initialize();
		$this->m = M('customer');
		
		
		
   }
 /**   聚餐美食-猜你喜欢  liuq
 *
 */	
  
   public  function edit_per(){
     
     $cid = I('cid');//  用户标识  member id
     $dataInfo = $this->m->where('cid="'.$cid.'"')->find(); 
    // $cus_heapic_name  = substr($dataInfo['cus_heapic'], 0, -1);
   //  $data1['cus_heapic'] = $cus_heapic_name?__VUPLOAD__.'heapic/'.$cus_heapic_name:"";
     $data1['cus_heapic'] = $this->m->where('cid="'.$cid.'"')->getField('cus_heapic')?__VUPLOAD__.'heapic/'.$this->m->where('cid="'.$cid.'"')->getField('cus_heapic'):"";
     $data1['nickname'] = $dataInfo['nickname'];
     $data1['cus_prv'] = M('shop_address')->where('id="'.$dataInfo['cus_prv'].'"')->getField('name');
     $data1['cus_city'] = M('shop_address')->where('id="'.$dataInfo['cus_city'].'"')->getField('name');
     $data1['cus_school'] = M('shop_address')->where('id="'.$dataInfo['cus_school'].'"')->getField('name');    
     $data1['major'] = $dataInfo['major'];   
     $data1['phone'] = $dataInfo['phone'];  
     if( $dataInfo['std_level'] == 1 ){
          $data1['std_level'] = '大一'; 
     }
     if( $dataInfo['std_level'] == 2 ){
          $data1['std_level'] = '大二'; 
     }
     if( $dataInfo['std_level'] == 3 ){
          $data1['std_level'] = '大三'; 
     }
     if( $dataInfo['std_level'] == 4 ){
          $data1['std_level'] = '大四'; 
     }
     
     if( $dataInfo['sex'] == 1){
          $data1['sex'] = '女'; 
     }
       if( $dataInfo['sex'] == 2){
          $data1['sex'] = '男'; 
     }
     
    returnApiData2('0','成功',$data1, $data2,$data3);exit;	     
       
   }
   
   
    public  function save_per(){
        
        $cid = I('cid');//  用户标识  member id
        $info['nickname'] = I('nickname');
        $info['cus_prv'] = I('cus_prv');
        $info['cus_city'] = I('cus_city');
        $info['cus_school'] = I('cus_school');
        $info['major'] = I('major');
        $info['phone'] =  I('phone');
        $info['std_level']= I('std_level');
        $info['sex'] = I('sex');
        $cus_heapic = I('cus_heapic');
        $info['cus_heapic'] = $this->sendpic($cus_heapic, 'Uploads/heapic/', $sign, $day);
        
        $result = $this->m->where('cid="'.$cid.'"')->save($info);
       if( $result ){
           returnApiresult("0", "成功");exit;
        }
        
    }
   
   
    
    //多图上传
 function sendpic($img,$address){
     
     if(!is_array($img)){
       $img=explode("|", $img);
       }
       for($i=0;$i<count($img);$i++){
           $newImgType= substr($img[$i],0,strrpos($img[$i],';'));  //图片的类型
           $base=substr($img[$i],strrpos($img[$i],';')+1);
           $a=explode(',',$base);
           $types=explode('/',$newImgType);
           $type=$types[1];
       //    dump($type);exit;
           $filename = rand(0,100000).time().".".$type; 
           $xmlstr=base64_decode($a[1]);
           makeDir($address);
           $file = fopen($address.$filename,"w");//打开文件准备写入  
           fwrite($file,$xmlstr);//写入
           fclose($file);//关闭
           $fileaddress[]=$filename;
//           $fileaddress[]=$filename;
       } 
       $info=implode('|',$fileaddress);
       return $info.'|';
    }

    

   public  function   list_address(){
       
      $cid = I('cid');
      $data = M('customer_shipping')->where('cid="'.$cid.'"')->order('add_time  desc')->select();
      foreach ($data as $key => $item){
          $data1[$key]['linkman'] = $item['linkman'];
          $data1[$key]['id'] = $item['id'];
          if( $item['sex'] == 1 ){
              $data1[$key]['sex'] = '女士';
          }  else {
              $data1[$key]['sex'] = '男士';
          }
           $data1[$key]['link_phone'] = $item['link_phone'];
           $data1[$key]['ship_prv'] = M('shop_address')->where('id="'.$item['ship_prv'].'"')->getField('name');
           $data1[$key]['ship_city'] = M('shop_address')->where('id="'.$item['ship_city'].'"')->getField('name');
           $data1[$key]['ship_school'] = M('shop_address')->where('id="'.$item['ship_school'].'"')->getField('name');
           
           $data1[$key]['ship_prv_id'] = $item['ship_prv'];
           $data1[$key]['ship_city_id'] = $item['ship_city'];
           $data1[$key]['ship_school_id'] = $item['ship_school'];
          
           
           $data1[$key]['ship_address'] = $item['ship_address'];
      } 
      
       
         returnApiData2('0','成功',$data1, $data2,$data3);exit;	     
         
   }
   
   
   
   public  function  add_address(){
   
      $info['cid'] = I('cid');
      $is_count = M('customer_shipping')->where('cid="'.$info['cid'].'"')->count();
      if( $is_count>=3){
           returnmemberError("1", "默认地址不能超过3个");exit;
      }
      $info['linkman']  = I('linkman');
      $info['sex'] = I('sex');//1女 2男
      $info['link_phone'] = I('link_phone');
      $info['ship_prv'] = I('ship_prv');
      $info['ship_city'] = I('ship_city');
      $info['ship_school'] = I('ship_school');
      $info['ship_address'] = I('ship_address');
      $info['add_time'] = time();
      $result = M('customer_shipping')->add($info);
      if( $result ){
           returnApiresult("0", "添加成功");exit;
      }
   }
   
   
   public  function edit_address(){
          $id = I('id');
            
         $dataInfo = M('customer_shipping')->where('id="'.$id.'"')->find();
         $data1['linkman'] = $dataInfo['linkman'];
         $data1['link_phone'] = $dataInfo['link_phone'];
         if( $dataInfo['sex'] == 1){
            $data1['sex'] = '女士'; 
         }
         if( $dataInfo['sex'] == 2){
             $data1['sex'] = '男士'; 
          }
          
          $data1['ship_prv'] = $dataInfo['ship_prv'];
          $data1['ship_city'] = $dataInfo['ship_city'];
          $data1['ship_school'] = $dataInfo['ship_school'];
          $data1['ship_address'] = $dataInfo['ship_address'];
        
          returnApiData2('0','成功',$data1, $data2,$data3);exit;	     
   }
   
   
  public function save_address(){
      $id = I('id');
      $info['linkman']  = I('linkman');
      $info['sex'] = I('sex');//1女 2男
      $info['link_phone'] = I('link_phone');
      $info['ship_prv'] = I('ship_prv');
      $info['ship_city'] = I('ship_city');
      $info['ship_school'] = I('ship_school');
      $info['ship_address'] = I('ship_address');
      $result = M('customer_shipping')->where('id="'.$id.'"')->save($info);
     
            returnApiresult("0", "编辑成功");exit;
           
      
      
  }
  
  
   public  function del_address(){
         $id = I('id');
         $result = M('customer_shipping')->where('id="'.$id.'"')->delete();
         
         if(  $result ){
            returnApiresult("0", "删除成功");exit;
           
       }  
   }
   
  
  
  public  function  my_evaluate(){
     $cid =  I('cid');
     $data = M('shop_evaluate')->where('cid="'.$cid.'"')->order('add_time  desc')->select();
     $count_num = M('shop_evaluate')->where('cid="'.$cid.'"')->order('add_time  desc')->count();
     foreach ($data as $key => $item){
     $data1[$key]['shop_name'] = M('shop')->where('mid="'.$item['mid'].'"')->getField('shop_name');    
         
     
      $every_star_total = $data[$key]['attitude_star'] + $data[$key]['speed_star'] +$data[$key]['taste_star'];
      $data[$key]['comment_star'] = round( $every_star_total/3 );
        
        if( $data[$key]['comment_star'] == 1 ){
             $data1[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."1star.jpg";
         }elseif(  $data[$key]['comment_star'] == 2  ){
             $data1[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."2star.jpg";
         }elseif(  $data[$key]['comment_star'] == 3  ){
             $data1[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."3star.jpg";
         }elseif(  $data[$key]['comment_star'] == 4  ){
             $data1[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."4star.jpg";
         }elseif(  $data[$key]['comment_star'] == 5  ){
             $data1[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."5star.jpg";
         }else{
             $data1[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."null.jpg";
         }
     
         
         $evaluate_pics = $item['evaluate_pics']; //evaluate_pics
         $datapics = explode("|", $evaluate_pics);
         $data[$key]["evaluate_pics"]=$datapics;
         foreach ($data[$key]["evaluate_pics"] as $key1 => $value) {
              $data1[$key]["evaluate_pics"][$key1]=__VUPLOAD__.'evaluate_pics/'.$value;
         }
         
         $data1[$key]['add_time']= $item['add_time'];
         $data1[$key]['evaluate'] = $item['evaluate'];
     
         
     } 
   
     $data2['phone'] = M('customer')->where('cid="'.$cid.'"')->getField('phone');
     $data2['cus_heapic'] =  M('customer')->where('cid="'.$cid.'"')->getField('cus_heapic')?__VUPLOAD__.'heapic/'.M('customer')->where('cid="'.$cid.'"')->getField('cus_heapic'):"";
   
     returnApiData3('0','成功',$data1, $data2,$data3,$count_num);exit;	     
      
  }
  
  
  public  function  feedback(){
      $info['cid'] = I('cid');
      $info['feedback'] = I('feedback');
      $info['add_time'] = I('add_time');
      $result = M('shop_feedback')->add($info);
      if( $result ){
           returnApiresult("0", "成功");exit;
      }
  }
  
 

  
    
                    public function up_pass_second(){
                      
                           $id = I('cid');
                          // $phone = I('phone');
                           $password = I('password');
                           $twicepass = I('twicepass');
                           if($password == '' || $twicepass == ''){
                                returnmemberError("1", "您的信息没有填写完整");exit;
                           }
                           if($twicepass != $password){
                                returnmemberError("1", "两次输入的密码不相符");exit;
                           }else{

                              $info['password'] = md5($password);
                              $result = M('member')->where('id="'.$id.'"')->save($info);
                                 if($result){
                                       returnApiresult('0','密码修改成功');exit;	
                                 }else{
                                      returnmemberError("1", "密码修改失败");exit;
                                 }
                          }

                   }

                   
                   
                     //修改密码  获取短信验证码  并存到session中
         public function up_sms(){
			$mobile = I('phone');
                        if(empty($mobile)){
			  returnmemberError("1", "电话号码不能为空");exit;
			}
                        $is_phone = M('member')->where('phone="'.$mobile.'"')->getField('id');
                        if( $is_phone == false){
                        returnmemberError("1", "该用户不存在");exit;
                        }
			$sms=$this->up_send_phone($mobile);
                       
		        if($sms){
                                returnApiresult('0','发送成功');exit;
                        }else{
                               returnmemberError("1", "发送验证码过于频繁");exit;
                        }
    	
		}
                
    
                public  function   up_pass(){
                             $phone = I('phone');
                             $code = I('code');
                             if($code == '' || $phone == ''){
                                returnmemberError("1", "您的信息没有填写完整");exit;
                             }
                             $is_phone = M('member')->where('phone="'.trim(I('phone')).'"')->getField('id');
                             if( $is_phone == false){
                                  returnmemberError("1", "该用户不存在");exit;
                             }else{
                                    $sms_mobile_code = F('liuq/up/'.$phone);
                                   if( $code !=  $sms_mobile_code ){
                                   returnmemberError("1", "验证码填写错误");exit;
                                   } else {
                                  // $data['phone'] = $phone;//放在第二页手机号位置
                                  // $data['cid'] = $is_phone;
                                  // returnApiData('0','成功',$data);exit;	
                                     returnApiresult("0", "成功");exit;
                                   }
                             }

                }

                   public function up_send_phone($tell){
                      import('Org.Alidayu.top.TopClient');
                      import('Org.Alidayu.top.ResultSet');
                      import('Org.Alidayu.top.RequestCheckUtil');
                      import('Org.Alidayu.top.TopLogger');
                      import('Org.Alidayu.top.request.AlibabaAliqinFcSmsNumSendRequest');
                      $c = new \TopClient;
                      $c->appkey = '23699407';  
                      $c->secretKey = '9db1f1f2185271c0ed07f5aed816fce1';
                      $req = new \AlibabaAliqinFcSmsNumSendRequest;
                      $req ->setExtend( "123456" );
                      $req ->setSmsType( "normal" );
                      $req ->setSmsFreeSignName( "学生惠" );//来源于配置短信签名 下面列表中有签名名称
                      //$code = '1234';
                      $code= rand(100000,999999);//随机六位验证码
                      F('liuq/up/'.$tell,$code);
                      $product = '学生惠密码';
                      $req ->setSmsParam( "{code:'{$code}',product:'{$product}'}" ); //变量来源于 配置短信模板 点击列表中的详情 模板内容的变量
                      $req ->setRecNum( $tell ); //手机号
                      $req ->setSmsTemplateCode("SMS_75850145"); //配置短信模板 列表中有模板id
                      $resp = $c ->execute( $req );
                      //var_dump($resp);
                      return $resp;
                  }
       
                  
                  
                  
                  
                  
                  
                  ////ios////////////////////////////////////////////////////////
                  
                  
                   public function up_sms_ios(){
			$mobile = I('phone');
                        if(empty($mobile)){
			  returnmemberError("1", "电话号码不能为空");exit;
			}
                        $is_phone = M('member')->where('phone="'.$mobile.'"')->getField('id');
                        if( $is_phone == false){
                        returnmemberError("1", "该用户不存在");exit;
                        }
			$sms=$this->up_send_phone_ios($mobile);
                        //$rcode=$sms[0];  //返回状态值
                        //  if( $rcode ==22){
			//   exit('1小时内同一手机号发送次数超过限制');
			//   }
			// $code=(int)$sms[1];   //返回验证码随机6位
		        if($sms){
                                returnApiresult('0','发送成功');exit;
                        }else{
                               returnmemberError("1", "发送验证码过于频繁");exit;
                        }
    	
		}
                
    
                public  function   up_pass_ios(){
                           $phone = I('phone');
                           $code = I('code');
                    
//                      $data['phone'] = I('phone');
//                           $data['code'] = I('code');
//                              $data['sess'] = $_SESSION['sms_mobile_code'];
//                              returnApiData2('0','成功',$data, $data2,$data3);exit;	     
                             if($code == '' || $phone == ''){
                                returnmemberError("1", "您的信息没有填写完整");exit;
                             }
                             $is_phone = M('member')->where('phone="'.trim(I('phone')).'"')->getField('id');
                             if( $is_phone == false){
                                  returnmemberError("1", "该用户不存在");exit;
                             }else{
                                  //  $sms_mobile_code = F('liuq/up/'.$phone);
                                   $sms_mobile_code = $_SESSION['sms_mobile_code'];
                                   if( $code !=  $sms_mobile_code ){
                                   returnmemberError("1", "验证码填写错误");exit;
                                   } else {
                                  // $data['phone'] = $phone;//放在第二页手机号位置
                                  // $data['cid'] = $is_phone;
                                  // returnApiData('0','成功',$data);exit;	
                                     returnApiresult("0", "成功");exit;
                                   }
                             }

                }

                   public function up_send_phone_ios($tell){
                      import('Org.Alidayu.top.TopClient');
                      import('Org.Alidayu.top.ResultSet');
                      import('Org.Alidayu.top.RequestCheckUtil');
                      import('Org.Alidayu.top.TopLogger');
                      import('Org.Alidayu.top.request.AlibabaAliqinFcSmsNumSendRequest');
                      $c = new \TopClient;
                      $c->appkey = '23699407';  
                      $c->secretKey = '9db1f1f2185271c0ed07f5aed816fce1';
                      $req = new \AlibabaAliqinFcSmsNumSendRequest;
                      $req ->setExtend( "123456" );
                      $req ->setSmsType( "normal" );
                      $req ->setSmsFreeSignName( "学生惠" );//来源于配置短信签名 下面列表中有签名名称
                      //$code = '1234';
                      $code= rand(100000,999999);//随机六位验证码
                     //  F('liuq/up/'.$tell,$code);
                      $_SESSION['sms_mobile_code'] = $code;
                      // $_SESSION['sms_mobile'] =  $tell;
                      $product = '学生惠';
                      $req ->setSmsParam( "{code:'{$code}',product:'{$product}'}" ); //变量来源于 配置短信模板 点击列表中的详情 模板内容的变量
                      $req ->setRecNum( $tell ); //手机号
                      $req ->setSmsTemplateCode("SMS_75850145"); //配置短信模板 列表中有模板id
                      $resp = $c ->execute( $req );
                      //var_dump($resp);
                      return $resp;
                  }
       
                  
                  
                  
                  
                  
                  
                

}